<?php

namespace CHOMP\SGIBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class WorkflowType extends AbstractType
{
	private $id;
	private $esAdmin;
	
	public function __construct($id, $esAdmin){
    	$this->id = $id;
		$this->esAdmin = $esAdmin;
	}    

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('hasta', 'entity', array(
						 'class'=>'CHOMPSGIBundle:Estado',
						 'property'=>'nombre',
						 'query_builder' => function (\CHOMP\SGIBundle\Entity\EstadoRepository $repository)
							{
							 return $repository->createQueryBuilder('u')
									->where('u.inicial = 0 AND u.borrado = 0');
							}	
						)
					)
           ->add('desde', 'entity', array(
						 'class'=>'CHOMPSGIBundle:Estado',
						 'property'=>'nombre',
						 'query_builder' => function (\CHOMP\SGIBundle\Entity\EstadoRepository $repository)
							{
							 return $repository->createQueryBuilder('u')
									->where('u.final = 0 AND u.borrado = 0');
							}	
						)
					);
			if($this->esAdmin == 'SI'){
				$builder->add('empresa', 'entity', array(
							 'class'=>'CHOMPSGIBundle:Empresa',
							 'property'=>'nombre',
							 'query_builder' => function (\CHOMP\SGIBundle\Entity\EmpresaRepository $repository)
								{
								 return $repository->createQueryBuilder('u')
										->where('u.borrado = 0');
								}
							)
						);
			}else{
				$builder->add('empresa', 'entity', array(
							 'class'=>'CHOMPSGIBundle:Empresa',
							 'property'=>'nombre',
							 'query_builder' => function (\CHOMP\SGIBundle\Entity\EmpresaRepository $repository)
								{
								 return $repository->createQueryBuilder('u')
										->where('u.id = ?1 AND u.borrado = 0')
										->setParameter(1, $this->id);
								}
							)
						);			
			}
			$builder->add('save', 'submit', array ('label' => 'Guardar', 'attr'=> array('class'=>'btn')));
     }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CHOMP\SGIBundle\Entity\Workflow'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'chomp_sgibundle_workflow';
    }
}
